package org.xx.armory.oss.autoconfigure;

import com.aliyun.oss.OSS;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.autoconfigure.AutoConfigureOrder;
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.boot.autoconfigure.condition.ConditionalOnWebApplication;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.Ordered;
import org.xx.armory.oss.OSSResource;
import org.xx.armory.oss.controllers.AliyunOssController;

/**
 * 自动配置{@literal armory.oss}。
 * <p>相关配置项：</p>
 * <dl>
 *     <dt>{@literal armory.aliyun-oss.endpoint}</dt>
 *     <dd>阿里云OBS的访问URL，和区域有关（比如华北一区）</dd>
 *     <dt>{@literal armory.aliyun-iam.access-key}</dt>
 *     <dd>阿里云IAM登录帐号的访问Key。</dd>
 *     <dt>{@literal armory.aliyun-iam.security-key}</dt>
 *     <dd>阿里云IAM登录帐号的密钥。</dd>
 *     <dt>{@literal armory.aliyun-oss.bucket}</dt>
 *     <dd>阿里云OBS的存储桶。OBS可以定义任意个存储桶，通常不同的项目使用不同的桶。</dd>
 * </dl>
 */
@Configuration
@AutoConfigureOrder(Ordered.HIGHEST_PRECEDENCE)
@ConditionalOnClass(OSS.class)
@ConditionalOnProperty("armory.aliyun-oss.endpoint")
public class AliyunOssAutoConfiguration {
    @Bean("ossClient")
    public OSSResource ossResource(
            @Value("${armory.aliyun-oss.access-key}") String accessKey,
            @Value("${armory.aliyun-oss.security-key}") String secretKey,
            @Value("${armory.aliyun-oss.endpoint}") String endPoint
    ) {
        return new OSSResource(endPoint, accessKey, secretKey);
    }

    @Bean
    @ConditionalOnWebApplication
    public AliyunOssController aliyunOssController() {
        return new AliyunOssController();
    }
}
